package com.hzhr.iot.device.aop.service;

import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.device.service.util.CheckUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.exception.ServiceException;

@Aspect
@Component
@Order(2)
public class CollectDevice1ServiceAspect extends BaseService {
    @Around("execution(* com.hzhr.iot.device.service.CollectDevice1Service.get(..))")
    public Object get(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        Map<String, String> header = (Map<String, String>) args[0];
        if (header == null) {
            header = new HashMap<>();
        }
        Map<String, Object> query = (Map<String, Object>) args[1];
        if (query == null) {
            query = new HashMap<>();
        }


        Map<String, String> headerTmp = new HashMap<>();
        if (!StringUtils.isEmpty(header.get("_hzhr-company-id"))) {
            headerTmp.put("_hzhr-company-id", header.get("_hzhr-company-id"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-dept-id"))) {
            headerTmp.put("_hzhr-dept-id", header.get("_hzhr-dept-id"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-user-type"))) {
            headerTmp.put("_hzhr-user-type", header.get("_hzhr-user-type"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-access-mode"))) {
            headerTmp.put("_hzhr-access-mode", header.get("_hzhr-access-mode"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-user-name"))) {
            headerTmp.put("_hzhr-user-name", header.get("_hzhr-user-name"));
        }
        if (!StringUtils.isEmpty(header.get("_token"))) {
            headerTmp.put("_token", header.get("_token"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-user-code"))) {
            headerTmp.put("_hzhr-user-code", header.get("_hzhr-user-code"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-transaction-id"))) {
            headerTmp.put("_hzhr-transaction-id", header.get("_hzhr-transaction-id"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-user-id"))) {
            headerTmp.put("_hzhr-user-id", header.get("_hzhr-user-id"));
        }
        if (!StringUtils.isEmpty(header.get("_hzhr-transaction-type"))) {
            headerTmp.put("_hzhr-transaction-type", header.get("_hzhr-transaction-type"));
        }
        if (!StringUtils.isEmpty(header.get("token"))) {
            headerTmp.put("_token", header.get("token"));
        }

        Map<String, Object> queryTmp = new HashMap<>();
        if (isInternalServiceInvok(headerTmp)) {
            if (!StringUtils.isEmpty(query.get("_page"))) {
                queryTmp.put("_page", query.get("_page"));
            } else if (!StringUtils.isEmpty(query.get("page"))) {
                queryTmp.put("_page", query.get("page"));
            }
            if (!StringUtils.isEmpty(query.get("_like_resourceCode"))) {
                queryTmp.put("_like_resourceCode", query.get("_like_resourceCode"));
            } else if (!StringUtils.isEmpty(query.get("like_resourceCode"))) {
                queryTmp.put("_like_resourceCode", query.get("like_resourceCode"));
            }
            if (!StringUtils.isEmpty(query.get("_show_total"))) {
                queryTmp.put("_show_total", query.get("_show_total"));
            } else if (!StringUtils.isEmpty(query.get("show_total"))) {
                queryTmp.put("_show_total", query.get("show_total"));
            }
            if (!StringUtils.isEmpty(query.get("_equal_id"))) {
                queryTmp.put("_equal_id", query.get("_equal_id"));
            } else if (!StringUtils.isEmpty(query.get("equal_id"))) {
                queryTmp.put("_equal_id", query.get("equal_id"));
            }
            if (!StringUtils.isEmpty(query.get("_show"))) {
                queryTmp.put("_show", query.get("_show"));
            } else if (!StringUtils.isEmpty(query.get("show"))) {
                queryTmp.put("_show", query.get("show"));
            }
            if (!StringUtils.isEmpty(query.get("_count"))) {
                queryTmp.put("_count", query.get("_count"));
            } else if (!StringUtils.isEmpty(query.get("count"))) {
                queryTmp.put("_count", query.get("count"));
            }
            if (!StringUtils.isEmpty(query.get("_equal_categoryCode"))) {
                queryTmp.put("_equal_categoryCode", query.get("_equal_categoryCode"));
            } else if (!StringUtils.isEmpty(query.get("equal_categoryCode"))) {
                queryTmp.put("_equal_categoryCode", query.get("equal_categoryCode"));
            }
            if (!StringUtils.isEmpty(query.get("_greaterthanorequal_installTime"))) {
                queryTmp.put("_greaterthanorequal_installTime", query.get("_greaterthanorequal_installTime"));
            } else if (!StringUtils.isEmpty(query.get("greaterthanorequal_installTime"))) {
                queryTmp.put("_greaterthanorequal_installTime", query.get("greaterthanorequal_installTime"));
            }
            if (!StringUtils.isEmpty(query.get("_show_info"))) {
                queryTmp.put("_show_info", query.get("_show_info"));
            } else if (!StringUtils.isEmpty(query.get("show_info"))) {
                queryTmp.put("_show_info", query.get("show_info"));
            }
            if (!StringUtils.isEmpty(query.get("_lessthan_installTime"))) {
                queryTmp.put("_lessthan_installTime", query.get("_lessthan_installTime"));
            } else if (!StringUtils.isEmpty(query.get("lessthan_installTime"))) {
                queryTmp.put("_lessthan_installTime", query.get("lessthan_installTime"));
            }
        } else {
            if (!StringUtils.isEmpty(query.get("equal_id"))) {
                queryTmp.put("_equal_id", query.get("equal_id"));
            }
            if (!StringUtils.isEmpty(query.get("like_resourceCode"))) {
                queryTmp.put("_like_resourceCode", query.get("like_resourceCode"));
            }
            if (!StringUtils.isEmpty(query.get("show_total"))) {
                queryTmp.put("_show_total", query.get("show_total"));
            }
            if (!StringUtils.isEmpty(query.get("show"))) {
                queryTmp.put("_show", query.get("show"));
            }
            if (!StringUtils.isEmpty(query.get("count"))) {
                queryTmp.put("_count", query.get("count"));
            }
            if (!StringUtils.isEmpty(query.get("equal_categoryCode"))) {
                queryTmp.put("_equal_categoryCode", query.get("equal_categoryCode"));
            }
            if (!StringUtils.isEmpty(query.get("greaterthanorequal_installTime"))) {
                queryTmp.put("_greaterthanorequal_installTime", query.get("greaterthanorequal_installTime"));
            }
            if (!StringUtils.isEmpty(query.get("show_info"))) {
                queryTmp.put("_show_info", query.get("show_info"));
            }
            if (!StringUtils.isEmpty(query.get("page"))) {
                queryTmp.put("_page", query.get("page"));
            }
            if (!StringUtils.isEmpty(query.get("lessthan_installTime"))) {
                queryTmp.put("_lessthan_installTime", query.get("lessthan_installTime"));
            }
        }

        if (isInternalServiceInvok(headerTmp)) {
            if (StringUtils.isEmpty(headerTmp.get("_token"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._token不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-user-id"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-user-id不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-user-code"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-user-code不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-user-name"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-user-name不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-company-id"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-company-id不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-dept-id"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-dept-id不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-user-type"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-user-type不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-transaction-id"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-transaction-id不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-transaction-type"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header._hzhr-transaction-type不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(headerTmp.get("_hzhr-access-mode"))) {
                headerTmp.put("_hzhr-access-mode", "0");
            }
            if (StringUtils.isEmpty(queryTmp.get("_show_total"))) {
                queryTmp.put("_show_total", "false");
            }
            if (StringUtils.isEmpty(queryTmp.get("_show_info"))) {
                queryTmp.put("_show_info", "false");
            }
            if (StringUtils.isEmpty(queryTmp.get("_page"))) {
                queryTmp.put("_page", "1");
            }
            if (StringUtils.isEmpty(queryTmp.get("_count"))) {
                queryTmp.put("_count", "1");
            }
        } else {
            if (StringUtils.isEmpty(headerTmp.get("_token"))) {
                Map<String, Object> objectInfo = new HashMap<>();
                objectInfo.put("header", headerTmp);
                throw new ServiceException(Common_ErrorsEnum.COMMON_6, "header.token不能为空", objectInfo);
            }
            if (StringUtils.isEmpty(queryTmp.get("_show_total"))) {
                queryTmp.put("_show_total", "false");
            }
            if (StringUtils.isEmpty(queryTmp.get("_show_info"))) {
                queryTmp.put("_show_info", "false");
            }
            if (StringUtils.isEmpty(queryTmp.get("_page"))) {
                queryTmp.put("_page", "1");
            }
            if (StringUtils.isEmpty(queryTmp.get("_count"))) {
                queryTmp.put("_count", "1");
            }
        }

        if (!StringUtils.isEmpty(queryTmp.get("_is_paging")) && !Boolean.parseBoolean(queryTmp.get("_is_paging").toString())) {
            queryTmp.put("_page", null);
            queryTmp.put("_count", null);
        }
        args[1] = queryTmp;
        args[0] = headerTmp;
        return joinPoint.proceed(args);
    }
}

